package cn.gqr.service.Impl;

import cn.gqr.entity.*;
import cn.gqr.mapper.HotMapper;
import cn.gqr.mapper.PublicMapper;
import cn.gqr.mapper.TypeMapper;
import cn.gqr.service.HotService;
import cn.gqr.service.PublicService;
import cn.gqr.tool.RespJson;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


@Service
public class PublicServiceImpl implements PublicService {

    @Autowired
    PublicMapper publicMapper;
    @Autowired
    HotService hotService;
    @Autowired
    TypeMapper typeMapper;

    //查询type类型的前num条数据
    @Override
    public List<Hot> findHot(int num, int type) {
        return publicMapper.findHot(num, type);
    }

    //根据类型分页查找
    @Override
    public List<NewBook> findBookByPage(int start, int num, int typeid) {
        return publicMapper.findBookByPage(start, num, typeid);
    }

    //模糊查询
    @Override
    public List<NewBook> findIf(int start, int num, String key) {
        return publicMapper.findIf(start, num, key);
    }

    //添加用户
    @Override
    public Map<String,Object> addUser(User user) {
        PasswordEncoder pe = new BCryptPasswordEncoder();
        user.setPassword(pe.encode(user.getPassword()));
        System.out.println(user);
        int i = publicMapper.addUser(user);
        return RespJson.pack(200,"添加成功..",i);
    }

    //点赞 rete加一
    @Override
    public int addRate(int bookid) {
        return publicMapper.addRate(bookid);
    }

    @Override
    public List<BookType> findAll2() {
        return typeMapper.findAll2();
    }


    //查询 高到低排序后的点赞,销量,加入购物车分页
    @Override
    public List<NewBook> findBookByPageTerm(String key, int start, int num, int typeId) {
        return publicMapper.findBookByPageTerm(key, start, num, typeId);
    }

    //查找 点赞,销量,加入购物车 前五条数据
    //key == 点赞/销量/加入购物车
    @Override
    public List<NewBook> findBefore5(String key) {
        System.out.println(key);
        return publicMapper.findBefore5(key);
    }

    //该类型数据的总行数
    @Override
    public int findLineByTypeid(int typeid) {
        return publicMapper.findLineByTypeid(typeid);
    }

    //短信注册
    @Override
    @Transactional
    public Map<String, Object> register(String phone, String phoneCode, HttpSession session) {
        //点击注册
        //在session中取出存储的手机号和验证码
        JSONObject json =(JSONObject)session.getAttribute("code");
        //把JSONObject对象转换为map
        Map<String, String> params = JSONObject.parseObject(json.toJSONString(), new TypeReference<Map<String, String>>(){});
        System.out.println(params);
        String memPhone = params.get("memPhone");
        String code = params.get("code");
        //判断验证是否通过
        if(memPhone.equals(phone) && phoneCode.equals(code)){
            //验证通过 判断该手机号是否注册//未注册
            if(publicMapper.ifRegister(memPhone)==null){
                //注册成功 添加数据库 手机号为账号 密码默认为123456
                User user = new User();
                user.setAccount(memPhone);
                BCryptPasswordEncoder pc = new BCryptPasswordEncoder();
                user.setPassword(pc.encode("123456"));
                user.setPhone(memPhone);
                user.setNickname("用户");
                int i = publicMapper.addUser(user);
                return RespJson.pack(200,"注册成功,快去登录吧...",i);
            }
            //已注册
            return RespJson.pack(202,"该手机号已注册",null);
        }
        return RespJson.pack(401,"验证失败,请重新获取验证码",null);
    }

    //模糊查询总行数
    @Override
    public int findByLine(int start, int num, String key) {
        return publicMapper.findByLine(start ,num ,key);
    }



}
